package com.billy.ohos.swipe.demo.consumer;

import com.billy.ohos.swipe.SmartSwipe;
import com.billy.ohos.swipe.SmartSwipeWrapper;
import com.billy.ohos.swipe.SwipeConsumer;
import com.billy.ohos.swipe.consumer.TranslucentSlidingConsumer;
import com.billy.ohos.swipe.demo.BaseAbility;
import com.billy.ohos.swipe.demo.ResourceTable;
import com.billy.ohos.swipe.listener.SimpleSwipeListener;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;

/**
 * image viewer (swipe down to finish this ability)
 *
 * @author billy.qi
 */
public class ImageViewerAbility extends BaseAbility {
    public static final String IMAGE_EXTRA = "extra_image";
    private Component abilityContentView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        int avatarResId = intent.getIntParam(IMAGE_EXTRA, 0);
        ComponentContainer parse = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_layout_image_view, null, false);
        Image imageView = (Image) parse.findComponentById(ResourceTable.Id_iv_image);
        if (avatarResId != 0) {
            imageView.setPixelMap(avatarResId);
        }
        setUIContent(parse);
        parse.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
        SmartSwipe.wrapView(imageView)
                .addConsumer(new TranslucentSlidingConsumer())
                .enableTop()
                .addListener(new SimpleSwipeListener() {
                    @Override
                    public void onSwipeStart(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction) {
//                            AbilityTranslucentUtil.convertAbilityToTranslucent(ImageViewerAbility.this, null);
                    }

                    @Override
                    public void onSwipeProcess(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction, boolean settling, float progress) {
                        Component contentView = wrapper.getContentView();
                        //set ohos.R.id.content alpha from opaque to translucent
//                            abilityContentView.setAlpha(1 - progress);
                        //set scale from 1 to 0
                        contentView.setScaleX(1 - progress);
                        contentView.setScaleY(1 - progress);
                    }

                    @Override
                    public void onSwipeOpened(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction) {
                        //finish this ability with no animation when swipe opened
                        terminateAbility();
//                            overridePendingTransition(R.anim.anim_none, R.anim.anim_none);
                    }
                });
//        }
    }
}
